﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;
using System.Text;

namespace LCA.Utils
{
    /// <summary>
    /// Summary description for StringHelper
    /// </summary>
    public class StringHelper
    {
        private static NumberFormatInfo FORMAT_PROVIDER_EN = CultureInfo.GetCultureInfo("en-US").NumberFormat;

        public static bool IsEmpty(string s)
        {
            return (s == null || s.Length == 0);
        }

        public static string GetString(string s)
        {
            return (s == null ? "" : s);
        }

        public static string GetString(object obj)
        {
            return (obj == null ? "" : obj.ToString());
        }

        public static string GetString(object obj, int nMaxLength)
        {
            if (obj == null)
            {
                return "";
            }
            if (obj.ToString().Length <= nMaxLength)
            {
                return obj.ToString();
            }
            return obj.ToString().Substring(0, nMaxLength);
        }

        public static string ToInitCap(string s)
        {
            if (s == null || s.Length < 1)
            {
                return "";
            }
            return Char.ToUpper(s[0]) + s.Substring(1).ToLower();
        }

        public static string FirstUpper(string s)
        {
            if (s == null || s.Length < 1)
            {
                return "";
            }
            return Char.ToUpper(s[0]) + s.Substring(1);
        }

        public static string FirstLower(string s)
        {
            if (s == null || s.Length < 1)
            {
                return "";
            }
            return Char.ToLower(s[0]) + s.Substring(1);
        }

        public static string ValueOf(int n, int len)
        {
            StringBuilder sb = new StringBuilder();
            string s = Convert.ToString(n);
            if (len != 0)
            {
                for (int i = len - s.Length; i > 0; i--)
                {
                    sb.Append("0");
                }
            }
            sb.Append(s);
            return sb.ToString();
        }

        public static string Truncate(string s, int maxlen)
        {
            return (s.Length <= maxlen) ? s : s.Substring(0, maxlen);
        }

        public static string ValueOf(long n, int len)
        {
            StringBuilder sb = new StringBuilder();
            string s = Convert.ToString(n);
            if (len != 0)
            {
                for (int i = len - s.Length; i > 0; i--)
                {
                    sb.Append("0");
                }
            }
            sb.Append(s);
            return sb.ToString();
        }

        public static string NCopyOf(string s, int count)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                sb.Append(s);
            }
            return sb.ToString();
        }

        public static string NCopyOf(char ch, int count)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                sb.Append(ch);
            }
            return sb.ToString();
        }

        public static string Trim(string s)
        {
            if (s != null)
            {
                return s.Trim();
            }
            return null;
        }

        public static DateTime? ParseDateTime(string s)
        {
            return ParseDateTime(s, null);
        }

        public static DateTime? ParseDateTime(string s, DateTime? _default)
        {
            DateTime result;
            if (DateTime.TryParse(s, FORMAT_PROVIDER_EN, DateTimeStyles.None, out result))
                return result;
            else
                return _default;
        }

        public static int? ParseInt(string s)
        {
            return ParseInt(s, null);
        }

        public static int? ParseInt(string s, int? _default)
        {
            int result;
            if (Int32.TryParse(s, NumberStyles.Integer, FORMAT_PROVIDER_EN, out result))
                return result;
            else
                return _default;
        }

        public static double? ParseDouble(string s)
        {
            return ParseDouble(s, null);
        }

        public static double? ParseDouble(string s, double? _default)
        {
            double result;
            if (Double.TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, FORMAT_PROVIDER_EN, out result))
                return result;
            else
                return _default;
        }

        public static bool? ParseBoolean(string s)
        {
            return ParseBoolean(s, null);
        }

        public static bool? ParseBoolean(string s, bool? _default)
        {
            bool result;
            if (Boolean.TryParse(s, out result))
                return result;
            else if (String.Equals(s, "yes", StringComparison.InvariantCultureIgnoreCase))
                return true;
            else if (String.Equals(s, "no", StringComparison.InvariantCultureIgnoreCase))
                return false;
            else if (s == "1")
                return true;
            else if (s == "0")
                return false;
            else
                return _default;
        }

        public static T ParseEnum<T>(string s, T _default)
        {
            try
            {
                return (T)Enum.Parse(typeof(T), s.Replace(" ", ""), true);
            }
            catch
            {
                return _default;
            }
        }

        public static Guid? ParseGuid(string s)
        {
            return ParseGuid(s, null);
        }

        public static Guid? ParseGuid(string s, Guid? _default)
        {
            Guid result;
            if (Guid.TryParse(s, out result))
                return result;
            else
                return _default;
        }

        public static decimal? ParseDecimal(string s)
        {
            return ParseDecimal(s, null);
        }

        public static decimal? ParseDecimal(string s, decimal? _default)
        {
            decimal result;
            if (Decimal.TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, FORMAT_PROVIDER_EN, out result))
                return result;
            else
                return _default;
        }
    }
}
